/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                       (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.

    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/

    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews

    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
package gpsnodelocator;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.StringTokenizer;

public class GPSNodeLocatorConfig 
{
	public static final String GVIA_COMMON_CONFIG_FILE = "/etc/gvia/gviaConfig.properties";
	private static final String GPS_NODE_LOCATOR_CONFIG_FILE = "gpsnodelocatorConfig.properties";
	protected static GPSNodeLocatorConfig singleton = null;
	private Properties gviaCommonProps;
	private Properties configProperties;
	private String[] incomingEvents;
	private String jdbcConnectionURI;
	private String user;
	private String password;
	
	private GPSNodeLocatorConfig() throws Exception
	{
		InputStream gviaIs = null;
		InputStream is = null;
		try
		{
			configProperties = new Properties();
			is = this.getClass().getResourceAsStream(GPS_NODE_LOCATOR_CONFIG_FILE);
			if (is == null)
				throw new Exception(GPS_NODE_LOCATOR_CONFIG_FILE + " file not found");
			configProperties.load(is);
			is.close();
			gviaCommonProps = new Properties();
			gviaIs = new FileInputStream(GVIA_COMMON_CONFIG_FILE);
			gviaCommonProps.load(gviaIs);
			gviaIs.close();
			setPassword();
			setUser();
			setJdbcConnectionURI();
			setIncomingEvents();
		}
		catch(Exception e)
		{
			if (is != null)
				is.close();
			if (gviaIs != null)
				gviaIs.close();
			throw e;
		}
	}

	public static GPSNodeLocatorConfig getConfig() throws Exception 
	{
		try
		{
			if (singleton == null)
			{
				singleton = new GPSNodeLocatorConfig();
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		return singleton;
	}

	public void setPassword() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("password");
		if (propValue == null)
			throw new Exception("password not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			password = propValue;
	}

	public String getPassword() 
	{
		return password;
	}

	public void setUser() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("user");
		if (propValue == null)
			throw new Exception("user not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			user = propValue;
	}

	public String getUser() 
	{
		return user;
	}
	
	private void setIncomingEvents() throws Exception
	{
		String propValue = configProperties.getProperty("incomingEvents");
		if (propValue == null)
		{
			throw new Exception("incomingEvents not added in " + GPS_NODE_LOCATOR_CONFIG_FILE + " file");
		}
		else
		{
			StringTokenizer st = new StringTokenizer(propValue, ",");
			ArrayList list = new ArrayList();
			while (st.hasMoreTokens())
			{
				list.add(st.nextToken());
			}
			incomingEvents = new String [list.size()];
			list.toArray (incomingEvents);
		}
	}
	
	public String[] getIncomingEvents() 
	{
		return incomingEvents;
	}
	
	private void setJdbcConnectionURI() throws Exception
	{
		String propValue = configProperties.getProperty("jdbcConnectionURI");
		if (propValue == null)
			throw new Exception("jdbcConnectionURI not added in " + GPS_NODE_LOCATOR_CONFIG_FILE + " file");
		else
			jdbcConnectionURI = propValue;
	}
	
	public String getJdbcConnectionURI()
	{
		return jdbcConnectionURI;
	}
}
